#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */

class Solution {
public:
    vector<vector<int>> pathSum(TreeNode* root, int targetSum)
    {
        if (root == nullptr)
        {
            return vector<vector<int>>();
        }

        _pathSum(root, 0, targetSum);
        return _ans;
    }

private:
    void _pathSum(TreeNode* node, int curSum, int targetSum)
    {
        _path.emplace_back(node->val);
        curSum += node->val;

        if (curSum == targetSum && !node->left && !node->right)
        {
            _ans.emplace_back(_path);
            _path.pop_back();
            return;
        }

        if (node->left)
        {
            _pathSum(node->left, curSum, targetSum);
        }

        if (node->right)
        {
            _pathSum(node->right, curSum, targetSum);
        }

        _path.pop_back();
    }

private:
    vector<vector<int>> _ans;
    vector<int> _path;
};